package com.wuye.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.wuye.dao.FangchanDaoImpl;
import com.wuye.dao.UsersDaoImpl;
import com.wuye.entity.Fangchan;

@Service("fangchanService")
public class FangchanService {
	@Resource
	private FangchanDaoImpl fangchanDao;
	@Resource
	private UsersDaoImpl userDao;

	public void save(Fangchan fangchan) {
		fangchan.setShijian(new Date());
		fangchanDao.save(fangchan);
	}

	public void del(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Fangchan fangchan = new Fangchan();
			fangchan.setId(Integer.parseInt(sz[i]));
			fangchanDao.del(fangchan);
		}
	}

	public void update(Fangchan fangchan) {
		fangchan.setShijian(new Date());
		fangchanDao.update(fangchan);
	}

	public Map getList(int offset, int length) {
		String[] title={"id","lou","danyuan","fanghao","shijian","name","userid"};
		return fangchanDao.getlist(title,"select f.id,f.lou ,f.danyuan,f.fanghao,substring(f.shijian,1,10),f.users.name,f.users.id from Fangchan f", offset, length);
	}

	public Map getSearchList(Fangchan fangchan, int offset, int length)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		String[] title={"id","lou","danyuan","fanghao","shijian","name","userid"};
		sb.append("select f.id,f.lou ,f.danyuan,f.fanghao,substring(f.shijian,1,10),f.users.name,f.users.id from Fangchan f");
		if(!fangchan.getLou().equals("")){
			sb.append(" where f.lou='"+fangchan.getLou()+"'");
			if(!fangchan.getDanyuan().equals("")){
				sb.append(" and f.danyuan='"+fangchan.getDanyuan()+"'");
			}
		}else{
			if(!fangchan.getDanyuan().equals("")){
				sb.append(" where f.danyuan='"+fangchan.getDanyuan()+"'");
			}
		}
		
		return fangchanDao.getlist(title, sb.toString(), offset, length);
	}
	
	public void goPage(HttpServletRequest request){
		request.setAttribute("list", userDao.getComboxList());
	}
	public FangchanDaoImpl getFangchanDao() {
		return fangchanDao;
	}

	public void setFangchanDao(FangchanDaoImpl fangchanDao) {
		this.fangchanDao = fangchanDao;
	}

	public UsersDaoImpl getUserDao() {
		return userDao;
	}

	public void setUserDao(UsersDaoImpl userDao) {
		this.userDao = userDao;
	}
}
